/**
 * 菜单JS
 */
angular.module('mall.search', [
])

.config(['$stateProvider', '$urlRouterProvider', function($stateProvider, $urlRouterProvider) {
	$stateProvider
		//搜索功能页
		.state('search', {
			url: '/search',
			templateUrl: cmt_mall_templateprefix+'/ngb/menu/search.html',
			resolve:{
				VillageList:['ShopService',function(ShopService){
					return ShopService.selectAllCountyShops(330329);
				}]
			},
			controller: 'SearchController'
		})
	;
}])

.controller('SearchController', ['$scope','$state','GoodsService','ShopService','VillageList',
function($scope, $state,GoodsService,ShopService,VillageList){
	$scope.villagelist = VillageList;
	$scope.shops=[];
	for(var i=0;i<$scope.villagelist.length;i++){
		$scope.villages=$scope.villagelist[i].shops;
		for(var j=0;j<$scope.villages.length;j++){
			$scope.shop=$scope.villages[j];
			$scope.shops.push($scope.shop)
		}
	}
	//console.log($scope.shops)
	//获取所有商品
	$scope.inner=function(shopid){//页面跳转
		//$rootScope.shopid = shopid;
		$state.go("goods",{shopid : shopid});
		//$state.go("ucmodel.goods");
	}
	/*change(1)*/
	focus()
	$scope.search = function(){
		var len=$scope.content.name.length;
		search_style(len);
	}
	$scope.cleansearch=function(){
		$scope.content.name=null
		hide_icon()
		focus()
	}
	$scope.Change=0;
	$(".products").hide()
	$scope.change_x= function (data) {
		$scope.Change=data.Change
		//console.log($scope.Change)
		if($scope.Change==0){
			$(".products").hide()
			$(".markets").show()
		}else if($scope.Change==1){
			$(".markets").hide()
			$(".products").show()
		}
	}
	/*//历史搜索
	$scope.historys=[]
	$scope.cleanhis=function(){
		$scope.historys=[]
	}
	$scope.searchbtn=function() {
		if($scope.content == undefined){
			return $scope.historys
		}else{
			//console.log($scope.historys.length+1)
			$scope.historys.push($scope.content)
			//console.log($scope.historys)
		}
	}*/
	$scope.searchbtn=function() {
		if($scope.content.name && $scope.Change==1) {
			$scope.goodslist = GoodsService.queryGoodsListByKeyword($scope.content.name);
			$scope.goodslist.$promise.then(function(goodslist){
				for(var i=0;i<goodslist.length;i++) {
					var goods = goodslist[i];
					goods.shopname = ShopService.queryShopByShopid(goods.shopid).name;
				}
			});
		}
	}
}])
;
